package com.xoa.service.cms;


import com.xoa.dao.cms.CmsChannelInfoMapper;
import com.xoa.dao.cms.CmsSiteInfoMapper;
import com.xoa.model.cms.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author 程叶同
* @date 2018/8/13 14:39
* @desc  cms树形结构解析
*/
@Service
public class CmsTreeNodeService {
    @Autowired
    CmsSiteInfoMapper cmsSiteInfoMapper;
    @Autowired
    CmsChannelInfoMapper channelInfoMapper;

    public List<TreeModel> treeNode(String id, String siteIds, String chnls){
        List<TreeModel> list = new ArrayList<TreeModel>();
        long childCount = 0;
        if(id==null && siteIds!=null){
            List<String> ids = Arrays.asList(siteIds);
            Map<String,Object> map=new HashMap<>();
            map.put("siteIds",ids);
            List<CmsSiteInfo> siteList =cmsSiteInfoMapper.getSteInfoByids(map);
            for(CmsSiteInfo si:siteList){
                TreeModel model = new TreeModel();
                model.setOpen(true);
                model.setName(si.getSiteName());
                model.setTitle(si.getSiteName());
                model.setIconSkin("site");
                model.setId(si.getSid()+";site");
                model.setExtend1(si.getSid()+"");
                //检测该站点下有无栏目
                CmsChannelInfoExample channelInfoExample=new CmsChannelInfoExample();
                CmsChannelInfoExample.Criteria criteria = channelInfoExample.createCriteria();
                criteria.andSiteIdEqualTo(si.getSid());
                criteria.andParentChnlEqualTo(0);
                List<CmsChannelInfo> cmsChannelInfos = channelInfoMapper.selectByExample(channelInfoExample);
                childCount = cmsChannelInfos.size();
                if(childCount!=0){
                    model.setParent(true);
                }else{
                    model.setParent(false);
                }
                list.add(model);
            }
        }else if(id==null){//展开站点信息

            List<CmsSiteInfo> siteList = cmsSiteInfoMapper.selectByExample(new CmsSiteInfoExample());
            for(CmsSiteInfo si:siteList){
                TreeModel model = new TreeModel();
                model.setOpen(true);
                model.setParent(true);
                model.setName(si.getSiteName());
                model.setTitle(si.getSiteName());
                model.setIconSkin("site");
                model.setId(si.getSid()+";site");
                model.setExtend1(si.getSid()+"");
                //检测该站点下有无栏目
                CmsChannelInfoExample channelInfoExample=new CmsChannelInfoExample();
                CmsChannelInfoExample.Criteria criteria = channelInfoExample.createCriteria();
                criteria.andSiteIdEqualTo(si.getSid());
                criteria.andParentChnlEqualTo(0);
                List<CmsChannelInfo> cmsChannelInfos = channelInfoMapper.selectByExample(channelInfoExample);
                childCount = cmsChannelInfos.size();
                if(childCount!=0){
                    model.setParent(true);
                }else{
                    model.setParent(false);
                }
                list.add(model);
            }
        }else{
            String sp [] = id.split(";");
            String sid = sp[0];
            String type = sp[1];
            List<CmsChannelInfo> chnlList = null;
            List<Integer> sids=new ArrayList<>();
            if(type.equals("site")){//展开该站点下的栏目
                if (chnls != null && chnls != "") {
                    String[] split = chnls.split(",");
                    if (split != null&&split.length>0) {
                        for (String s : split) {
                            if (s!=null&&s!="") {
                                Integer n= Integer.parseInt(s);
                                sids.add(n);
                            }
                        }
                    }
                }
                CmsChannelInfoExample channelInfoExample=new CmsChannelInfoExample();
                CmsChannelInfoExample.Criteria criteria = channelInfoExample.createCriteria();
                criteria.andSiteIdEqualTo(Integer.parseInt(sid));
                criteria.andDelFlagEqualTo(0);
                if (chnls!=null) {
                    criteria.andSidNotIn(sids);
                }
                criteria.andParentChnlEqualTo(0);
                channelInfoExample.setOrderByClause("SORT_NO ASC");
                chnlList = channelInfoMapper.selectByExample(channelInfoExample);
            }else if(type.equals("chnl")){//展开该栏目下的子栏目
                if (chnls != null && chnls != "") {
                    String[] split = chnls.split(",");
                    if (split != null&&split.length>0) {
                        for (String s : split) {
                            if (s!=null&&s!="") {
                                Integer n= Integer.parseInt(s);
                                sids.add(n);
                            }
                        }
                    }
                }
                CmsChannelInfoExample channelInfoExample=new CmsChannelInfoExample();
                CmsChannelInfoExample.Criteria criteria = channelInfoExample.createCriteria();
                criteria.andParentChnlEqualTo(Integer.parseInt(sid));
                criteria.andDelFlagEqualTo(0);
                if (chnls!=null) {
                    criteria.andSidNotIn(sids);
                }
                channelInfoExample.setOrderByClause("SORT_NO ASC");
                chnlList = channelInfoMapper.selectByExample(channelInfoExample);
            }

            for(CmsChannelInfo ch:chnlList){
                TreeModel model = new TreeModel();
                model.setOpen(true);
                model.setParent(true);
                model.setName(ch.getChnlName());
                model.setTitle(ch.getChnlName());
                model.setIconSkin("channel");
                model.setId(ch.getSid()+";chnl");
                model.setExtend1(ch.getSiteId()+"");
                //检测该站点下有无栏目
                CmsChannelInfoExample channelInfoExample=new CmsChannelInfoExample();
                CmsChannelInfoExample.Criteria criteria = channelInfoExample.createCriteria();
                criteria.andSiteIdEqualTo(ch.getSiteId());
                criteria.andParentChnlEqualTo(0);
                criteria.andDelFlagEqualTo(0);
                criteria.andParentChnlEqualTo(ch.getSid());
                List<CmsChannelInfo> cmsChannelInfos = channelInfoMapper.selectByExample(channelInfoExample);
                childCount = cmsChannelInfos.size();
                if(childCount!=0){
                    model.setParent(true);
                }else{
                    model.setParent(false);
                }
                list.add(model);
            }

//			if(!type.equals("rootSite")){
//
//			}else{
//				List<SiteInfo> siteList = simpleDaoSupport.find("from SiteInfo where sid="+sid+" order by sortNo asc", null);
//				for(SiteInfo si:siteList){
//					TeeZTreeModel model = new TeeZTreeModel();
//					model.setOpen(true);
//					model.setParent(true);
//					model.setName(si.getSiteName());
//					model.setTitle(si.getSiteName());
//					model.setIconSkin("site");
//					model.setId(si.getSid()+";site");
//					model.setExtend1(si.getSid()+"");
//					list.add(model);
//				}
//			}

        }
        return list;
    }
}
